package com.wj.controller;

import com.wj.responese.ResultResponse;
import com.wj.services.NumberCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * &#064;create  2021-01-04 16:04
 * &#064;description  数字验证码服务可以根据传入的参数控制数字验证码长度
 */
@RestController
public class NumberCodeController {

    @Autowired
    private NumberCodeService numberCodeService;

    @GetMapping("/number-code/{length}")
    public ResultResponse<String> numberCode(@PathVariable int length) {
        try {
            String randomNumber = numberCodeService.generateCode(length);
            return ResultResponse.success(randomNumber);
        } catch (IllegalArgumentException e) {
            return ResultResponse.failure(e.getMessage());
        }
    }
}
